package com.imay.live.activity.family.controller;

import android.app.Activity;
import android.view.View;

import com.imay.live.R;
import com.imay.live.utils.ViewUtil;
import com.imay.live.view.refresh.RefreshView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public abstract class BaseFamilyController {

    protected Activity activity;
    protected TwinklingRefreshLayout refresh_view;

    public static class Builder<T extends BaseFamilyController> {
        private final Activity activity;
        private final Class clazz;

        public static BaseFamilyController.Builder create(Activity activity, Class<? extends BaseFamilyController> clazz) {
            if (activity == null) {
                throw new NullPointerException("activity == null");
            }
            return new BaseFamilyController.Builder(activity, clazz);
        }

        protected Builder(Activity activity, Class<? extends BaseFamilyController> clazz) {
            this.activity = activity;
            this.clazz = clazz;
        }

        public BaseFamilyController build() {
            BaseFamilyController controler = null;
            try {
                controler = (T) clazz.newInstance();
                controler.initView(activity);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return controler;
        }
    }

    private void initView(Activity activity) {
        this.activity = activity;
        ViewUtil.setForgroundRadius(ViewUtil.findView(activity, R.id.content));

        initView();
    }

    protected void initButtom(boolean canLoadmore) {
        if (refresh_view != null) {
            LoadingView bottomProgressView = new LoadingView(activity);
            refresh_view.setBottomView(bottomProgressView);
            refresh_view.setEnableLoadmore(canLoadmore);
            refresh_view.setOverScrollBottomShow(false);
        }
    }

    protected void initRreshView(int rid) {
        refresh_view = ViewUtil.findView(activity, rid);
    }

    protected void initRreshHead() {
        if (refresh_view != null) {
            RefreshView headerView = new RefreshView(activity);
            refresh_view.setHeaderView(headerView);
            refresh_view.setEnableRefresh(true);
            refresh_view.setOverScrollTopShow(false);
            refresh_view.setEnableOverScroll(false);
        }
    }

    public void finishRresh() {
        if (refresh_view != null) {
            refresh_view.finishRefreshing();
        }
    }

    public void finishLoadMore() {
        if (refresh_view != null) {
            refresh_view.finishLoadmore();
        }
    }

    protected abstract void initView();
}
